package com.codeyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codeyang.domain.ProdTag;
import com.codeyang.service.ProdTagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 描述:
 *
 * @author CodeYang_Site
 * @version 2021/5/25 14:14
 */
@Api("商品标签分组的接口管理")
@RestController
@RequestMapping("prod/prodTag")
public class ProdTageController  {


    @Autowired
    private ProdTagService prodTagService;


    @ApiOperation("分组标签分页查询")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:prodTag:page')")
    public ResponseEntity<Page<ProdTag>> info(Page<ProdTag> page, ProdTag prodTag) {
        // title 模糊查询 staus 精确查询

        Page<ProdTag> prodTagPage = prodTagService.page(page,
                new LambdaQueryWrapper<ProdTag>()
                        .eq(!ObjectUtils.isEmpty(prodTag.getStatus()), ProdTag::getStatus, prodTag.getStatus())
                        .like(StringUtils.hasText(prodTag.getTitle()), ProdTag::getTitle, prodTag.getTitle())
                        .orderByDesc(ProdTag::getSeq)
        );

        return ResponseEntity.ok(prodTagPage);
    }


    @GetMapping("listTagList")
    @ApiOperation("全查询商品标签分组")
    @PreAuthorize("hasAuthority('prod:prodTag:page')")
    public ResponseEntity<List<ProdTag>> listTagList() {
        List<ProdTag> list = prodTagService.list();
        return ResponseEntity.ok(list);
    }

    @PostMapping
    @ApiOperation("新增一个商品的标签")
    @PreAuthorize("hasAuthority('prod:prodTag:save')")
    public ResponseEntity<Void> save(@RequestBody @Validated ProdTag prodTag) {
        prodTagService.save(prodTag);
        return ResponseEntity.ok().build();
    }


    @DeleteMapping("/{id}")
    @ApiOperation("删除一个商品标签")
    @PreAuthorize("hasAuthority('prod:prodTag:delete')")
    public ResponseEntity<Void> delete(@PathVariable("id") Long id) {
        prodTagService.removeById(id);
        return ResponseEntity.ok().build();
    }


    @DeleteMapping
    @ApiOperation("删除多个商品标签")
    @PreAuthorize("hasAuthority('prod:prodTag:delete')")
    public ResponseEntity<Void> delete(@RequestBody List<Long> ids) {
        prodTagService.removeByIds(ids);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/info/{id}")
    @ApiOperation("标签数据的回显")
    @PreAuthorize("hasAuthority('prod:prodTag:info')")
    public ResponseEntity<ProdTag> findById(@PathVariable("id") Long id) {
        ProdTag prodTag = prodTagService.getById(id);
        return ResponseEntity.ok(prodTag);
    }

    //----- 实现远程调用服务提供-------


    @GetMapping("prodTagList")
    @ApiOperation("查询商品前台标签分组")
    public ResponseEntity<List<ProdTag>> loadProdTags() {
        List<ProdTag> list = prodTagService.loadFrontProteanList();
        return ResponseEntity.ok(list);
    }
}
